<?
//引入所有子系统信息
include_once("../../../common/script/extender_import.php");


//**获取当前板块资料，必然载入，后续很多读取板块数据库的表格，都写成动态的了。
include_once("script/this_plate.php");


//根据页面数量信息，筛选信息，创造url，供页码,跳转等使用
include_once("script/set_url.php");




//price 格式化 小数点后两位
//$_REQUEST['ai_price'] = number_format($_REQUEST['ai_price'],2);

//todo 记录 sql操作语句
//todo 记录修改版本
$_data=select($table_prefix.$this_plate['table']['category'],$_REQUEST['nid'])[0];











//这里是设计上传文件的部分：包含图片和附件。视频也应该用这种形式上传
    //统一设置一下保存图片的路径
    $_uf['path']        = ROOT_PATH_ . "/uploadfiles/".$theme_folder."/";

    //创建删除文件存放文件夹
    if(!file_exists($_uf['path'] ."/delete/")){
        mkdir($_uf['path'] ."/delete/",0777,true);
        chmod($_uf['path'] ."/delete/",0777);
    }


    //上传附件
    if (is_uploaded_file($_FILES['attachment']['tmp_name'])) {
        $_uf["allowExt"] = array('jpeg',
            'jpg',
            'png',
            'gif',
            'rar',
            'zip',
            '7zip',
            'exe',
            'msi',
            'txt',
            'doc',
            'xlsx',
            'pptx',
            'pdf',
            'webp');
        $_uf['upfile']             = $_FILES['attachment'];
        $_info                     = upload_file($_uf);
        $_REQUEST['ai_attachment'] = $_info['name'];
        if($_REQUEST['ai_attachment']){
            rename($_uf['path'].$old_detail['attachment'], $_uf['path'] ."delete/".$old_detail['attachment']);
        }
    }



//处理数据并且update
//获取数据

$sql=update_date($table_prefix.$this_plate['table']['category'],$_REQUEST,"nid",$_REQUEST['nid']);
$query = execute_query($sql) ;



//******************************************
//判断是否更新成功，然后把图片从temp目录移动出来
//******************************************


if($query)
{
    $_image_upload_list_to_check =
        ['icon',
            'image1','image2','image3','image4','image5',
            'image6','image7','image8','image9','image10',
            'video', 'banner', 'attachment', 'extend_img'];

//循环判断检查所有要上传的图片，去做移动
    foreach ($_image_upload_list_to_check as $value)
    {
        if(isset($_REQUEST['ai_'.$value]))
        {

            $source      = ROOT_PATH_ . "/uploadfiles/temp/".$_REQUEST['ai_'.$value];
            $destination = ROOT_PATH_ . "/uploadfiles/".$_REQUEST['ai_'.$value];

            if (file_exists($source))
            {
                if (copy($source, $destination))
                {
                    unlink($source); // 删除源文件
                }
                else
                {
                    error_log("Failed to copy file from $source to $destination");
                }
            }


            //******************************************
            //获取新数据的图片，，用于对比删除
            //******************************************

            if($old_detail[$value] != $_REQUEST['ai_'.$value])
            {

                $source      = ROOT_PATH_ . "/uploadfiles/".$old_detail[$value];
                $destination = ROOT_PATH_ . "/uploadfiles/delete/".$old_detail[$value];

                if (copy($source, $destination))
                {
                    unlink($source); // 删除源文件
                }

            }


        }
    }
}

//******************************************
//判断是否更新成功，然后把图片从temp目录移动出来
//******************************************

?>

